<template>
  <el-container class="homm_container">
    <!-- 头部区域 -->
    <el-header>
      <div class="logo">
        <!-- <img src="../assets/logo.png" alt="" /> -->
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="signOut">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '70px' : '200px'">
        <div class="toggle_button" @click="toggleCollapse">
          <i class="el-icon-more"></i>
        </div>
          <!-- 侧边栏菜单区域 -->
        <el-menu :collapse="isCollapse" :collapse-transition="false" :unique-opened='true' router :default-active='activePaths'
         background-color="#333744" text-color="#fff" active-text-color="#ffd04b">
        <!-- 一级菜单 -->
          <el-submenu v-for="item in menulist" :index="item.id + ''" :key="item.id">
              <!-- 一级菜单的模板区域 -->
            <template slot="title">
                <!-- 图标 -->
              <i :class="iconList[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item v-for="item2 in item.children" :index="'/' + item2.path" :key="item2.id" @click="activePath('/' + item2.path)">
                <template slot="title">
                    <!-- 图标 -->
                    <i class="el-icon-menu"></i>
                    <!-- 文本 -->
                    <span>{{item2.authName}}</span>
                </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view/>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单数据
      menulist: [],
      iconList: {
        125: 'el-icon-user-solid',
        103: 'el-icon-s-order',
        101: 'el-icon-s-cooperation',
        102: 'el-icon-s-data',
        145: 'el-icon-s-platform'
      },
      // 是否折叠
      isCollapse: false,
      // 被激活的链接地址
      activePaths: ''
    }
  },
  created () {
    this.getMenList()
    this.activePaths = window.sessionStorage.getItem('activePath')
  },
  methods: {
    signOut () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      // console.log(this.menulist)
    },
    // 点击按钮切换菜单的折叠与展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    activePath (path) {
      window.sessionStorage.setItem('activePath', path)
      this.activePaths = path
    }
  }
}
</script>

<style scoped>
.el-header{
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.el-aside{
    background-color: #333744;
}
.el-main{
    background-color: #eaedf1;
}
.homm_container{
    height: 100%;
}
.logo{
    display: flex;
    align-items: center;
}
.logo img{
    width: 30px;
}
.logo span{
    color: #fff;
}
.el-menu{
  border-right: none;
}
.toggle_button{
  background-color: #4A5064;
}
.toggle_button i{
    color: #fff;
    text-align: center;
    display: block;
    font-size: 32px;
    cursor: pointer;
}
</style>
